//
//  LSAddAlipayUserID.m
//  HappyStore
//
//  Created by 陈锋 on 16/8/16.
//  Copyright © 2016年 LLJ. All rights reserved.
//

#import "LSAddAlipayUserID.h"

@interface LSAddAlipayUserID (){

    NSString *fileName;
    NSMutableArray * dataArr;

}
@property (weak, nonatomic) IBOutlet UITextField *alipayUserID;
@property (weak, nonatomic) IBOutlet UITextField *nameTX;
@property (weak, nonatomic) IBOutlet UIButton *sureBtn;

@end

@implementation LSAddAlipayUserID

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"添加账户";
    
    dataArr = [NSMutableArray array];
    fileName=[DOCUMENTPATHS stringByAppendingPathComponent:@"alipayUserIDFile.plist"];
    
    self.sureBtn.layer.masksToBounds = YES;
    self.sureBtn.layer.cornerRadius = 5.0f;
    
    //开启侧滑
    self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;
}

- (IBAction)sureBtn:(UIButton *)sender {
    
    if (((self.alipayUserID.text.length != 0 && [self isValidateEmail:self.alipayUserID.text]) || (self.alipayUserID.text.length
                                                                                                  
        != 0 && [self isMobileNumber:self.alipayUserID.text])) && ([self.nameTX.text isChinese] && self.nameTX.text.length >=2)) {
        
        NSMutableDictionary * dict = [NSMutableDictionary  dictionary];
        dict[@"alipayUserID"] = self.alipayUserID.text;
        dict[@"nameTX"] = self.nameTX.text;
        
        [dataArr addObject:dict];
        [dataArr writeToFile:fileName atomically:YES];
        
        self.alipayUserID.text = @"";
        self.nameTX.text = @"";
        
        [self.navigationController popViewControllerAnimated:YES];
        
    }else{
        
        NSString *strMsg = @"请输入正确的账户信息";
        
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"添加情况" message:strMsg delegate:self cancelButtonTitle:@"知道了" otherButtonTitles:nil, nil];
        [alert show];
        
    }

    
}

- (BOOL)isChinese
{
    NSString *match = @"(^[\u4e00-\u9fa5]+$)";
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF matches %@", match];
    return [predicate evaluateWithObject:self];
}

// 正则判断手机号码地址格式
- (BOOL)isMobileNumber:(NSString *)mobileNum {
    
    //    电信号段:133/153/180/181/189/177
    //    联通号段:130/131/132/155/156/185/186/145/176
    //    移动号段:134/135/136/137/138/139/150/151/152/157/158/159/182/183/184/187/188/147/178
    //    虚拟运营商:170
    
    NSString *MOBILE = @"^1(3[0-9]|4[57]|5[0-35-9]|8[0-9]|7[06-8])\\d{8}$";
    
    NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];
    
    return [regextestmobile evaluateWithObject:mobileNum];
}

-(BOOL)isValidateEmail:(NSString *)email {
    NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    return [emailTest evaluateWithObject:email];
}
@end
